package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.ProductDiscussion;
import com.clkj.xiuzheng.service.shangcheng.ScDiscussionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/ScDiscussion")
@CrossOrigin
@Api(value = "/ScDiscussion", tags = {"Z商城问答管理"})
public class ScDiscussionController {

    @Autowired
    private ScDiscussionService scDiscussionService;

    @ApiOperation(value = "根据(公司id<可选>和parentId<可选>和商品名称<可选>和问答类型<可选>)获取问答列表")
    @PostMapping("/getDiscussionByCompanyId")
    public Result getDiscussionByCompanyId(@ApiParam(name = "companyId", value = "公司id")@RequestParam(required = false) Integer companyId,
                                           @ApiParam(name = "parentId", value = "问题id")@RequestParam(required = false) Integer parentId,
                                           @ApiParam(name = "productId", value = "商品id")@RequestParam(required = false) Integer productId,
                                           @ApiParam(name = "discussionType", value = "0问 1答") @RequestParam(required = false) Integer discussionType,
                                           @RequestParam Integer pageSize,
                                           @RequestParam Integer pageNum) {
            try {
                PageInfo page = scDiscussionService.getDiscussionByCompanyId(companyId, parentId,productId,discussionType, pageSize, pageNum);
                return new Result(true, StatusCode.OK,"查询成功",page);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, StatusCode.ERROR,"查询失败");
            }
    }
    @ApiOperation(value = "添加问答")
    @PostMapping("/addDiscussion")
    public Result addDiscussion(@RequestBody ProductDiscussion productDiscussion) {
        try {
            scDiscussionService.addDiscussion(productDiscussion);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false, StatusCode.ERROR,"添加失败");
       }
   }
    @ApiOperation(value = "删除问答")
    @PostMapping("/deleteDiscussion")
    public Result deleteDiscussion(@ApiParam(name = "Ids", value = "问答字符串id ,")String Ids) {
        try {
            scDiscussionService.deleteDiscussion(Ids);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
    @ApiOperation(value = "置顶问答")
    @PostMapping("/topDiscussion")
    public Result topDiscussion(@ApiParam(name = "Ids", value = "问答字符串id ,")String Ids,@ApiParam(name = "isTop", value = "1置顶0取消置顶")Integer isTop) {
        try {
            scDiscussionService.topDiscussion(Ids,isTop);
            return new Result(true, StatusCode.OK,"置顶成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"置顶失败");
        }
    }




}
